/**
 * Created by John on 2016/7/1.
 */
var demo = new Sample();

// demo.math();
// demo.increment();
// demo.moduleCustom();
// demo.exports();
// demo.compile();
// demo.dump();
// demo.node();

function Sample() {
    this.math = function () {
        var math = require('math');
    };

    this.sum = function () {
        var sum = 0,
            i = 0,
            args = arguments,
            length = args.length;
        while (i < length) {
            sum += args[i++];
        }
        return sum;
    };

    this.increment = function (val) {
        return this.sum(val, 1)
    };

    this.moduleCustom = function (id, parent) {
        this.id = id;
        this.exports = {};
        this.parent = parent;
        if (parent && parent.children) {
            parent.children.push(this);
        }
        this.filename = null;
        this.loaded = false;
        this.children = [];

        this.extensions['.json'] = function (module, filename) {
            var content = NativeModule.require('fs').readFileSync(filename, 'utf-8');
            try {
                module.exports = JSON.parse(stripBOM(content));
            } catch (err) {
                err.message = filename + ': ' + err.message;
                throw err;
            }
        }
    };

    this.extensions = function () {
        console.log(require.extensions);
    };

    this.compile = function () {
        return (function (exports, require, module, __filename, __dirname) {
            var math = require('math');
            exports.area = function (radius) {
                return Math.PI * radius * radius;
            };
        });
    };

    this.exports = function () {
        var change = function (a) {
            a = 100;
            console.log(a);
        };

        var a = 10;
        change(a);
        console.log(a);
    };

    this.dump = function () {
        // namespace node {
        //     const char node_native[] = {47, 47, ...};
        //     const char dgram_native[] = {47, 47, ...};
        //     const char console_native[] = {47, 47, ...};
        //     const char buffer_native[] = {47, 47, ...};
        //     const char querystring_native[] = {47, 47, ...};
        //     const char punycode_native[] = {47, 47, ...};
        //     ...
        //
        //     struct _native {
        //         const char* name;
        //         const char* source;
        //         size_t source_len;
        //     };
        //
        //     static const struct _native natives[] = {
        //         {"node", node_native, sizeof(node_native)-1},
        //         {"dgram", dgram_native, sizeof(dgram_native)-1},
        //         ...
        //     };
        // }
    };

    this.core = function () {
        function NativeModule(id) {
            this.filename = id + '.js';
            this.id = id;
            thi.exports = {};
            this.loaded = false;
        }

        NativeModule._source = process.binding('natives');
        NativeModule._cache = {};
    };

    this.node = function () {
        // struct node_module_struct {
        //     int version;
        //     void *dso_handle;
        //     const char *filename;
        //     void (*register_func) (v8::Handle<v8::Object> target);
        //     const char *modname;
        // };
        //
        // #define NODE_MODULE (modname, regfunc)
        //     extern "C" {
        //         NODE_MODULE_EXPORT node::node_module_struct modname ## _module =
        //         {
        //             NODE_STANDARD_MODULE_STUFF,
        //             regfunc,
        //             NODE_STRINGIFY (modname)
        //         };
        // }
    }
}